using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using System.Diagnostics;
using System.Xml;
using System.Xml.XPath;
using TypeDataset;

namespace MaskUserControl
{
    public partial class NavigationTree : UserControl
    {
		private string _XMLFileName = "";
        public event OnSelectNodeChange OnSelectChangeEvent;
        public NavigationTree()
        {
            InitializeComponent();
        }
		public string XMLFile
		{ 
			get 
			{
				return _XMLFileName;
			}
			set
			{
				_XMLFileName = value;
				InitNode(_XMLFileName);
			}
		}
        private void InitNode(string fileName)
        {
			XPathDocument doc = new XPathDocument(fileName);
			XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
			XPathNodeIterator iter = nav.Select("/NodeRoot/ViewNode");
			_TreeList.Nodes.Clear();
			while (iter.MoveNext())
			{
				string caption = iter.Current.GetAttribute("Caption", "");

				TreeListNode node = _TreeList.AppendNode(new object[] { caption }, -1);
				XPathNodeIterator iterChild = iter.Current.SelectChildren(XPathNodeType.Element);

				while (iterChild.MoveNext())
				{
					string nodeCaption = iterChild.Current.GetAttribute("Caption", "");
					TreeListNode childNode = _TreeList.AppendNode(new object[] { nodeCaption }, node);
					MaskNagivationNode maskNode = new MaskNagivationNode();
					maskNode.FieldName = iterChild.Current.GetAttribute("FieldName", "");
					maskNode.FieldValue = iterChild.Current.GetAttribute("FieldValue", "");
					maskNode.FieldType = iterChild.Current.GetAttribute("FieldType", "");
					childNode.Tag = maskNode;
				}
			}
			_TreeList.ExpandAll();
        }


        private void _TreeList_Click(object sender, EventArgs e)
        {
            TreeListMultiSelection selection = _TreeList.Selection;

            if (selection.Count != 0)
            {
                TreeListNode node = selection[0];
				MaskNagivationNode tag;
				if (node.Tag != null)
					tag = (MaskNagivationNode)node.Tag;
				else
					tag = new MaskNagivationNode();
				if (OnSelectChangeEvent != null)
					OnSelectChangeEvent(tag);
            }
        }
    }
    public delegate void OnSelectNodeChange(MaskNagivationNode viewNode);

}
